//package com.example.qwen02.controller;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//import org.apache.http.HttpEntity;
//import org.apache.http.client.methods.CloseableHttpResponse;
//import org.apache.http.client.methods.HttpGet;
//import org.apache.http.client.methods.HttpPost;
//import org.apache.http.entity.StringEntity;
//import org.apache.http.impl.client.CloseableHttpClient;
//import org.apache.http.impl.client.HttpClients;
//import org.apache.http.util.EntityUtils;
//import org.springframework.web.bind.annotation.*;
//import org.springframework.web.multipart.MultipartFile;
//
//import java.io.File;
//import java.io.IOException;
//import java.util.HashMap;
//import java.util.Map;
//
////@RestController
////@RequestMapping("/ai")
////@CrossOrigin(origins = "*")
//public class WxiangController02 {
//    @GetMapping(value="/getImage",produces = "application/json;charset=utf-8")
//    public String getImage(String task_id) throws IOException{
//        // 创建HttpClient实例
//        CloseableHttpClient httpClient = HttpClients.createDefault();
//
//        // 创建HttpGet对象，指定URL
////        String taskId="197043f3-0db9-4867-8830-04358e2198f0"; //可根据实际替换不同的taskId
//        String url = "https://dashscope.aliyuncs.com/api/v1/tasks/"+task_id;
//
//        HttpGet httpGet = new HttpGet(url);
//
//        httpGet.setHeader("Authorization", "Bearer " + System.getenv("DASHSCOPE_API_KEY"));
//
//        try {
//            // 执行HTTP Get请求
//            CloseableHttpResponse response = httpClient.execute(httpGet);
//            // 处理响应
//            HttpEntity entity = response.getEntity();
//            String result = EntityUtils.toString(entity);
//            System.out.println("Response:" + result);
//            response.close();
//            return result;
//        } finally {
//            httpClient.close();
//        }
//    }
//    @PostMapping("/askStyleChange")
//    public Map<String, Object> askStyleChange(String or_url,int style)throws IOException{
//        System.out.println("这是or_url"+or_url);
//        // 创建HttpClient实例
//        CloseableHttpClient httpClient = HttpClients.createDefault();
//
//        // 创建HttpPost对象，指定URL
//        String url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/image-generation/generation";
//        HttpPost httpPost = new HttpPost(url);
//        httpPost.setHeader("X-DashScope-Async", "enable");
//        httpPost.setHeader("Authorization", "Bearer " + System.getenv("DASHSCOPE_API_KEY"));
//        httpPost.setHeader("Content-Type", "application/json");
//
//        // JSON参数
//        String jsonParam = "{\n" +
//                "   \"model\": \"wanx-style-repaint-v1\",\n" +
//                "   \"input\": {\n" +
//                "       \"image_url\": \""+or_url+"\",\n" +
//                "       \"style_index\": "+style+"\n" +
//                "   }\n" +
//                "}";
//        // 将JSON字符串转换为HttpEntity对象并设置到HttpPost中
//        httpPost.setEntity(new StringEntity(jsonParam));
//
//        try {
//            // 执行HTTP POST请求
//            CloseableHttpResponse response = httpClient.execute(httpPost);
//            // 处理响应
//            HttpEntity entity = response.getEntity();
//            String result = EntityUtils.toString(entity);
//            System.out.println("Response:" + result);
//            response.close();
//
//            // result是json格式，要提取出其中的task_id
//            // 解析整个JSON字符串为一个JSONObject
//            JSONObject jsonObject = JSON.parseObject(result);
//
//            Map<String, Object> map = new HashMap<>();
//            if (jsonObject.containsKey("output")) {
//                // 从JSONObject中获取名为"output"的子对象
//                JSONObject outputObject = jsonObject.getJSONObject("output");
//                // 从outputObject中提取task_id字段的值
//                String taskId = outputObject.getString("task_id");
//                map.put("state","success");
//                map.put("task_id", taskId);
//            }else{
//                map.put("state","error");
//                map.put("message",jsonObject.getString("message"));
//            }
//            return map;
//        } finally {
//            httpClient.close();
//        }
//    }
//    @PostMapping("/upload")
//    public Map<String, Object> uploadPhoto(MultipartFile photo) throws IOException {
//        photo.transferTo(new File("E:\\Vue\\photos\\"+photo.getOriginalFilename()));
//        Map<String, Object> map = new HashMap<>();
//        map.put("url", "/ai/photos/"+photo.getOriginalFilename());
//        return map;
//    }
//}
